package com.charm.bulkcomponent.vassonic

import android.content.Context
import android.content.Intent
import com.tencent.sonic.sdk.SonicSession
import com.tencent.sonic.sdk.SonicSessionConnection
import java.io.BufferedInputStream
import java.lang.ref.WeakReference
import com.tencent.sonic.sdk.SonicConstants


/**
 * Created by tools on 2018/1/25.
 */
class OfflinePkgSessionConnection(context: Context, session: SonicSession, intent: Intent) : SonicSessionConnection(session, intent) {
    private val context: WeakReference<Context> = WeakReference(context)
    override fun getResponseHeaderFields(): MutableMap<String, MutableList<String>> {
        return HashMap(0)
    }

    override fun internalConnect(): Int {
        val ctx = context.get()
        if (null != ctx) {
            try {
                val offlineHtmlInputStream = ctx.assets.open("sonic-demo-index.html")
                responseStream = BufferedInputStream(offlineHtmlInputStream)
                return SonicConstants.ERROR_CODE_SUCCESS
            } catch (e: Throwable) {
                e.printStackTrace()
            }

        }
        return SonicConstants.ERROR_CODE_UNKNOWN
    }

    override fun getResponseCode(): Int {
        return 200
    }

    override fun internalGetResponseStream(): BufferedInputStream {
        return responseStream;
    }

    override fun disconnect() {
    }

    override fun getResponseHeaderField(key: String?): String {
        return ""
    }

}